/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.LayerConfig = function(config) {

	this.availablelayertree = new GBIF.GeoSpatial.LayerTree({
			split: true
		,	region: 'west'
		,	width: 300
		,	minWidth:270
		,	maxWidth:310
		,	rootVisible: true
		,	rootExpanded:true
		,	checkboxdisabled:true
//		,	cls:'x-tree-icon'
/*
		,	root: new Ext.tree.AsyncTreeNode({
					text: 'Layers'
				,	draggable: false
				,	expanded: true
				,	rootVisible: true
				,	type: 'fld'
				,	layer: ''
				,	id: 'Layers'
			})
*/		
	});
	
	this.availablelayertree.savetreeconfig.on('click', this.saveTreeConfig, this);
	this.availablelayertree.overwriteconfig.on('click', this.overwriteConfig, this);

	this.wizardPanel = new GBIF.GeoSpatial.WizardPanel({
		region:'center'
	});

	this.treeConfigWin = new Ext.Window({
			layout: "fit"
		,	hideBorders: true
		,	closeAction: "hide"
		,	modal:true
		,	width: 650
		,	height: 450
		,	resizeble:false
		,	border:false
		,	title: "Tree Configuration"
		,	items: [{
					xtype: "form"
				,	layout: "fit"
				,	autoScroll: true
				,	scope: this
				,	border: false
				,	ref: 'treeconfigform'
				,	items: [{
							xtype: "textarea"
						,	ref:'treeconfig'
						,	scope:this	
					}]
			}]
		,	buttons: [{
					text: "Overwrite Config"
				,	scope:this
				,	ref:'../overwriteconfig'
				,	iconCls:'icon-overwritelayer'		
			}, {
					text: "Close"
				,	listeners: {
						click: {
								scope: this
							,	fn: function(){
									this.treeConfigWin.hide();
								}
						}
					}	
			}]
	});
	
	this.treeConfigWin.overwriteconfig.on('click',	this.overwriteConfig,	this);
				
	this.wizardPanel.on('addLayerNode', this.addLayerNode, this);

	Ext.apply(this, config, {
			border: false
		,	layout: 'border'
		,	defaults: {border: false}
		,	items: [
					this.availablelayertree
				, this.wizardPanel
			]
	});

	GBIF.GeoSpatial.LayerConfig.superclass.constructor.call(this, config);
			
}

Ext.extend(GBIF.GeoSpatial.LayerConfig, Ext.Panel, {
		
		overwriteConfig:function(){
			this.createJson();
			var json = Ext.encode(this.layersConfig[0].children);
			
			Ext.MessageBox.confirm('Overwrite Config File', 'Are you sure you wish to overwrite Config file?', function(e) {
				if (e == 'yes') {
					Ext.Ajax.request({
							url: 'resources/api/json-file-rewrite.php'
						,	method: 'POST'
						,	timeout: 120000
						,	scope: this
						,	params: {
								layers: json 
							}
						,	success: function(responseObject) {
								var response = Ext.decode(responseObject.responseText);
								if (response.success) {
									GBIF.GeoSpatial.Notice.msg(this.txtSucess, this.txtSucessMsg);
								}else{
									GBIF.GeoSpatial.Notice.msg(this.txtNotice, this.txtNoticeMsg);	
								}
							}
						,	failure: function() {
								GBIF.GeoSpatial.Notice.msg(this.txtNotice, this.txtNoticeMsg);
							}
					});
				}
			}, this);
		}
	
	,	saveTreeConfig: function() {
			this.createJson();
			this.treeConfigWin.treeconfigform.treeconfig.setValue( Ext.encode(this.layersConfig[0].children) );
			this.treeConfigWin.show();
		}
   
	,	buildLayersConfig: function(node, mirror){
			var cs = node.childNodes;
			node.attributes.loader = '';
			if(node.attributes.layer!= ''){
				node.attributes.checked=false;
			}

			mirror = Ext.apply(mirror, node.attributes);
			
			for(var i = 0, len = cs.length; i < len; i++) {
				if (i == 0) {
					mirror.children = [];
				}
				mirror.children.push({});
				this.buildLayersConfig( cs[i], mirror.children[i]);
			}
		}

	,	createJson: function(){
			this.layersConfig = [];
			this.layersConfig.push({});
			this.buildLayersConfig( this.availablelayertree.getRootNode(), this.layersConfig[0]);
		}
   
	,	addLayerNode:function(data){
			switch(data.mType) {
				case 'wms':
					if(data.queryable){
						this.addWMSFeatureLayerNode(data);
					} else {
						this.addWMSLayerNode(data);
/*						
						Ext.each(data.layer, function(mapLayer){
							mapLayer.description = data.description;
							this.addWMSLayerNode(mapLayer);
						}, this);	
*/						
					}
					break;

				case 'kml':
				case 'georss':
					this.addOtherLayerNode(data);
					break;

				case 'wfs':
					var layer=data.layer;
					Ext.each(layer,function(maplayer){
						this.addWFSLayerNode(maplayer);
					},this);
					break;	

				case 'gbif.clb':
					this.addGBIFLayerNode(data);
					break;

				case 'birdlife':
					this.addbirdlifeLayerNode(data);
					break;	
			}
		}
			
	,	addWMSLayerNode: function(data){
			var root = this.availablelayertree.getRootNode();
			var presentnode = true;
			Ext.each(root.childNodes, function(node){
				if(node.text == data.name){
					presentnode = false;		
				}
			});
					
			if (presentnode) {
//console.log("add" , data);
				var node = root.appendChild(new Ext.tree.TreeNode({
						text: data.name
//					,	id: data.name	
					,	leaf: true
					,	expanded: true
					,	description: data.description
					,	mType: 'wms'
					,	onlineResource: data.onlineResource
					,	layer: {	
								layers: data.layer.params.LAYERS
							,	name: data.name
							,	url: data.layer.url
							,	version: data.layer.params.VERSION
							,	features: ''
							,	querable: false
							,	opacity: 1
							,	transparent: "TRUE"
							,	FORMAT: data.layer.params.FORMAT
						}
				}));
				GBIF.GeoSpatial.Notice.msg("Notice","Layer has been added.");		
			} else {
				return;
			}				
		}			

	,	addWMSFeatureLayerNode:function(data){
			var root = this.availablelayertree.getRootNode();
			var presentnode = true;
			Ext.each(root.childNodes,function(node){
				if(node.text == data.title){
					presentnode=false;		
				}
			});
				
			if (presentnode) {
				var node = root.appendChild(new Ext.tree.TreeNode({
						text: data.title
					,	id: data.title	
					,	leaf: true
					,	expanded: true
					,	description: ''
					,	mType: 'wms'
					,	layer: {	
								layers: data.name
							,	name: data.name
							,	url: data.layer.url
							,	version: ''
							,	features: data.layer.features ||''
							,	opacity: 1
							,	querable: true
							,	ransparent: true
							,	FORMAT: data.layer.params.FORMAT
							}
				}));
				GBIF.GeoSpatial.Notice.msg("Notice","Layer has been added.");		
			} else {
				return;
			}				
		}

	,	addWFSLayerNode:function(data){
			var root = this.availablelayertree.getRootNode();
			var presentnode=true;
			Ext.each(root.childNodes,function(node){
				if(node.text==data.name){
					presentnode = false;
				}
			});

			if (presentnode) {
				var node = root.appendChild(new Ext.tree.TreeNode({
						text: data.name
					,	id: data.name
					,	leaf: true
					,	expanded: true
					,	mType: 'wfs'
					,	description: ''
					,	layer: {
								layers: data.name
							,	name: data.name
							,	version: data.protocol.version
							,	url: data.protocol.url
							,	featureType: data.protocol.featureType
							,	featureNS: data.protocol.featureNS
						}
				}));
				GBIF.GeoSpatial.Notice.msg("Notice","Layer has been added.");		
			} else {
				return;
			}	
		}			
	
	,	addOtherLayerNode:function(data){
			var root = this.availablelayertree.getRootNode();
			var node = root.appendChild(new Ext.tree.TreeNode({
					text: data.name
				,	id: data.name	
				,	leaf:true
				,	expanded:true
				,	description:data.description
				,	mType:data.mType
				,	layer: {	
							layers: data.title
						,	name: data.name
						,	url: data.url
						,	version: ''
						,	opacity: 1
						,	transparent: "TRUE"
						,	FORMAT: 'png'
					}
			}));
			GBIF.GeoSpatial.Notice.msg("Notice","Layer has been added.");		
		}
		
	,	addGBIFLayerNode:function(data){
			var root = this.availablelayertree.getRootNode();
			var node = root.appendChild(new Ext.tree.TreeNode({
					text: data.name
				,	leaf: false
				,	id: data.name
				,	expanded: false
				,	mType: 'wms'
				,	description: data.description
				,	layer: ''
				,	type: 'gbif.clb'
			}));
			GBIF.GeoSpatial.Notice.msg("Notice","Layer has been added.");		
		}	
	
	,	addbirdlifeLayerNode:function(data){
			var root = this.availablelayertree.getRootNode();
			var node = root.appendChild(new Ext.tree.TreeNode({
					text: data.name
				,	leaf:false
				,	id:data.name
				,	expanded:false
				,	mType:'wfs'
				,	description: data.description
				,	layer:''
				,	nType:'family'
				,	type:'birdlife'
			}));	
			GBIF.GeoSpatial.Notice.msg("Notice","Layer has been added.");
	}					
				
});